package com.example.imitation_webchat_demo.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.imitation_webchat_demo.R;
import com.example.imitation_webchat_demo.activity.ChatActivity;
import com.example.imitation_webchat_demo.adapter.MessageAdapter;
import com.example.imitation_webchat_demo.controller.MessageHandler;
import com.example.imitation_webchat_demo.entity.MessageItem;
import com.example.imitation_webchat_demo.entity.NoReadMesInfo;
import com.example.imitation_webchat_demo.model.Resource;
import com.example.imitation_webchat_demo.util.AppUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

// 消息页面fragment
public class MessageFragment extends Fragment {
	@ViewInject(R.id.message_listview)
	private ListView listView;
	
	@ViewInject(R.id.message_searchview)
	private EditText searchText;
	
	@ViewInject(R.id.message_search_clear)
	private ImageButton searchClearBtn;

	private MessageAdapter adapter;
	private List<MessageItem> list;
	private MessageItem item;
	
	private String username;
	private String nickname;
	private int noReadSum;    //未读的消息数
	private String NoReadMestype;
	private NoReadMesInfo info;
	
	private MessageHandler messageHandler;
	private int sum;
	
	public void setMessageHandler(MessageHandler messageHandler){
		this.messageHandler = messageHandler;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_message, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		ViewUtils.inject(this, getActivity());

		initComponent(); // 初始化控件
		addListener(); // 添加监听
	}

	// 初始化控件
	public void initComponent() {
		noReadSum = 0;
		NoReadMestype = "no read message";
		username = AppUtils.getUsernameFromShare(getActivity());
		nickname = getActivity().getSharedPreferences(Resource.appName, Context.MODE_PRIVATE).getString("nickname", null);
		
		list = new ArrayList<MessageItem>();
		for (int i = 0; i < 4; i++) {
			int itemCount = 2;
			item = new MessageItem();
			item.setImageId(R.drawable.ic_launcher);
			item.setTitle(i + "某某某");
			item.setMessage("对方发来一条信息");
			item.setTime("上午11:00");
			item.setNumber(itemCount);
			list.add(item);
			
			if(item.getNumber() > 0){
				noReadSum += itemCount;
			}
		}
		adapter = new MessageAdapter(getActivity(), list);
		listView.setAdapter(adapter);
		listView.setTextFilterEnabled(true);
	}

	// 添加监听
	public void addListener() {
		// 点击listview每一项的响应
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				MessageItem item = list.get(position);
				int number = item.getNumber();
				if(number > 0){
					if(noReadSum > 0){
						noReadSum -= item.getNumber();
						info = new NoReadMesInfo();
						info.setNumber(noReadSum);
						info.setType(NoReadMestype);
						messageHandler.onReceive(info);
					}
					item.setNumber(0);
					adapter.notifyDataSetChanged();
				}
				Intent intent = new Intent(MessageFragment.this.getActivity(),
						ChatActivity.class);
				intent.putExtra("username", item.getTitle());
				MessageFragment.this.getActivity().startActivity(intent);
			}
		});
		
		searchText.addTextChangedListener(new TextWatcher(){
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String str = s.toString();
				adapter.getFilter().filter(s);
				if(str != null && str.length() != 0){
					searchClearBtn.setVisibility(View.VISIBLE);
				}else{
					searchClearBtn.setVisibility(View.INVISIBLE);
				}
			}

			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
	}
	
	//点击搜索栏里的删除按钮，删除搜索栏中的文本
	@OnClick(R.id.message_search_clear)
	private void clear(View view){
		searchText.setText("");
	}
}